/**
 * Created by liuzengrong on 2019/8/16.
 */
import React from 'react';
import { connect } from 'dva';
import StandardModule from '@/components/hyperspace/modules/StandardModule';
import ResourceRights from './components/ResourceRights';


let resourceRights: any = {};

/**
 * 自定义模块
 */
// @ts-ignore
const LocalModule: React.FC = ({ resource, dispatch }) => (
  <div>
    <StandardModule
      data={resource.data}
      dictionary={[
      ]}
      config={{
        common: {
          formItemOptions: {
            resCode: {
              rules: [{ required: true, message: '资源编码不能为空！' }],
            },
            resName: {
              rules: [{ required: true, message: '资源名称不能为空！' }],
            },
            weight: {
              rules: [{ required: true, message: '权重不能为空！' }],
            },
            resType: {
              rules: [{ required: true, message: '资源类型不能为空！' }],
            },
            status: {
              rules: [{ required: true, message: '状态不能为空！' }],
            },
          },
        },
        search: {
          fields: ['resCode', 'resName'], // 查询条件
        },
        list: {
          url: '/api/resource/query',
          fields: [
            'id',
            'resCode',
            'resName',
            'parentCode',
            'parentName',
            'icon',
            'weight',
            'resType',
            'status',
            'creator',
            'creatTime',
          ], // 列表字段
          format: {
            username: {
              render: (value: any) => `格式化 - ${value}`,
            },
          },
          extensions: {
            operations: [
              (record: any) => (
                <span>
                    <a
                      onClick={() => {
                        if (resourceRights) resourceRights.handleOpen();
                      }}
                    >
                      权限配置
                    </a>
                </span>
              ),
            ],
          },
        },
        add: {
          url: '/api/resource/create',
          // width: 680, // 非必填，默认680px
          // title: '新建表单', // 非必填，默认`新建`
          fields: [
            'resCode',
            'resName',
            'parentCode',
            'icon',
            'weight',
            'resType',
            'status',
          ], // 新增表单字段
        },
        update: {
          url: '/api/resource/update',
          // width: 680, // 非必填，默认680px
          // title: '新建表单', // 非必填，默认`新建`
          fields: [
            'resCode',
            'resName',
            'parentCode',
            'icon',
            'weight',
            'resType',
            'status',
          ], // 修改表单字段
          primaryKeys: ['id'], // 主键字段列表
        },
        delete: {
          url: '/api/resource/remove',
          batchDelete: false, // 是否开启批量删除
          fields: ['id'], // 删除依据字段
        },
      }}
      namespace="resource"
      dispatch={dispatch}
    />
    {/* eslint-disable-next-line no-return-assign */}
    <ResourceRights wrappedComponentRef={(inst: any) => (resourceRights = inst)}
      {...resourceRights}
      visible
    />
  </div>
);

// @ts-ignore
export default connect(({ resource }) => ({
  resource,
}))(LocalModule);
